package com.summer.ioc.bean.definition;

import com.summer.ioc.exception.BeanException;
import lombok.Getter;
import lombok.Setter;

import java.util.Set;

/**
 * @author xiaoye
 * @create 2021-04-29 15:15
 */
@Getter
@Setter
public abstract class AbstractBeanDefinition implements BeanDefinition{

    protected String name;

    protected Class beanClass;

    protected Object beanObject;

    protected String scope = BeanScope.SINGLETON;

    protected boolean lazyLoad = false;

    protected Set<String> dependencyBeanNames = null;

    public abstract Set<String> getDependencyBeanNames();

    public static class BeanScope{
        public static final String
            SINGLETON = "singleton",
            PROTOTYPE = "prototype";
    }

    public AbstractBeanDefinition(String name, Class beanClass) {
        this.name = name;
        this.beanClass = beanClass;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public Object getObject() {
        return beanObject;
    }


    @Override
    public Class getBeanClass() {
        return beanObject == null? beanClass: beanObject.getClass();
    }

    @Override
    public <T> T getObject(Class<T> beanClass) {
        if (beanClass.isAssignableFrom(beanObject.getClass()))
            return (T)beanObject;
        else
            throw new BeanException("bean '"+name+"' of type ["+ beanObject.getClass().getName() +"] can not cast to type [" + beanClass.getName() + "]" );
    }


}
